{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## RDF\n",
    "The radial distribution function (RDF) denoted in equations by g(r), defines the probability of finding a particle at a distance r from another tagged particle. The RDF strongly depends on the matter type so it will vary significantly for solids, gases and liquids. \n",
    "<img src=\"../images/rdf.png\" width=\"60%\" height=\"60%\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As you might have observed the code complexity of the algorithm in $N^{2}$ . Let us get into details of the sequential code. **Understand and analyze** the code present at:\n",
    "\n",
    "<details>\n",
    "    <summary markdown=\"span\"><b>C/C++ code</b></summary>\n",
    "    \n",
    "[RDF Serial Code](../source_code/rdf.cpp)\n",
    "\n",
    "[File Reader](../source_code/dcdread.h)\n",
    "    \n",
    "</details>\n",
    "<br/>\n",
    "\n",
    "\n",
    "<details>\n",
    "    <summary markdown=\"span\"><b>Fortran code</b></summary>\n",
    "\n",
    "[RDF Serial Code](../source_code/rdf.f90)\n",
    "    \n",
    "</details>\n",
    "<br/>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!cd ../source_code && make clean && make rdf_c rdf_f"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We plan to follow the typical optimization cycle that every code needs to go through\n",
    "<img src=\"../images/workflow.png\" width=\"70%\" height=\"70%\">\n",
    "\n",
    "To analyze the application, we will  use the  profiler \"nsys\" and add the \"nvtx\" marking into the code to get more information out of the serial code. Before running the below cells, let's first start by divining into the profiler lab to learn more about the tools. Using Profiler gives us the hotspots and helps to understand which function is important to be made parallel.\n",
    "\n",
    "-----\n",
    "\n",
    "# <div style=\"text-align: center ;border:3px; border-style:solid; border-color:#FF0000  ; padding: 1em\">[Nsight Systems Overview](nsight_systems.ipynb)</div> \n",
    "\n",
    "# <div style=\"text-align: center ;border:3px; border-style:solid; border-color:#FF0000  ; padding: 1em\">[Nsight Compute Overview](nsight_compute.ipynb)</div> \n",
    "\n",
    "-----\n",
    "\n",
    "Now that we are familiar with the Nsight Profiler and know how to [NVTX](nsight_systems.ipynb#nvtx), let's profile the serial code and review the output."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# profiling C/C++ code\n",
    "!cd ../source_code && nsys profile -t nvtx --stats=true --force-overwrite true -o rdf_serial ./rdf_c"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Once you run the above cell, you should see the following in the terminal.\n",
    "\n",
    "<img src=\"../images/serial.png\" width=\"70%\" height=\"70%\">\n",
    "\n",
    "To view the profiler report, download and save the report file by holding down <mark>Shift</mark> and <mark>right-clicking</mark> the [C/C++ version](../source_code/rdf_serial.nsys-rep) then choosing <mark>save Link As</mark> Once done, open it via the GUI. For more information on how to open the report via the GUI, please checkout the section on [How to view the report](nsight_systems.ipynb#gui-report). \n",
    "\n",
    "From the _Timeline View_, right click on the nvtx row and click the \"show in events view\". Now you can see the nvtx statistic at the bottom of the window which shows the duration of each range. In the following labs, we will look in to the profiler report in more detail. \n",
    "\n",
    "<img src=\"../images/nvtx_serial.png\" width=\"100%\" height=\"100%\">\n",
    "\n",
    "The obvious next step is to make **Pair Calculation** algorithm parallel using different approaches to GPU Programming. Please follow the below link and choose one of the approaches to parallelise th serial code.\n",
    "\n",
    "-----\n",
    "\n",
    "# <div style=\"text-align: center ;border:3px; border-style:solid; border-color:#FF0000  ; padding: 1em\">[HOME](../_start_nways_C_Fortran.ipynb)</div> \n",
    "\n",
    "-----\n",
    "\n",
    "\n",
    "# Links and Resources\n",
    "<!--[OpenACC API guide](https://www.openacc.org/sites/default/files/inline-files/OpenACC%20API%202.6%20Reference%20Guide.pdf)-->\n",
    "\n",
    "[NVIDIA Nsight System](https://docs.nvidia.com/nsight-systems/)\n",
    "\n",
    "[NVIDIA Nsight Compute](https://docs.nvidia.com/nsight-compute/)\n",
    "\n",
    "<!--[NVIDIA Nsight Compute](https://developer.nvidia.com/nsight-compute)-->\n",
    "\n",
    "<!--[CUDA Toolkit Download](https://developer.nvidia.com/cuda-downloads)-->\n",
    "\n",
    "[Profiling timelines with NVTX](https://devblogs.nvidia.com/cuda-pro-tip-generate-custom-application-profile-timelines-nvtx/)\n",
    "\n",
    "**NOTE**: To be able to see the Nsight Systems profiler output, please download the latest version of Nsight Systems from [here](https://developer.nvidia.com/nsight-systems).\n",
    "\n",
    "Don't forget to check out additional [Open Hackathons Resources](https://www.openhackathons.org/s/technical-resources) and join our [OpenACC and Hackathons Slack Channel](https://www.openacc.org/community#slack) to share your experience and get more help from the community.\n",
    "\n",
    "--- "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Licensing \n",
    "\n",
    "Copyright © 2022 OpenACC-Standard.org.  This material is released by OpenACC-Standard.org, in collaboration with NVIDIA Corporation, under the Creative Commons Attribution 4.0 International (CC BY 4.0). These materials may include references to hardware and software developed by other entities; all applicable licensing and copyrights apply."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
